/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package predictor;

import dataanalysis.Model;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.util.zip.GZIPInputStream;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import weka.core.Instances;

/**
 *
 * @author Administrator
 */
public class MainView {

    private final MainApp m_manager;
    private final MainFrame m_mainFrame = new MainFrame("CBIB Predictor", this);
    private final ProgressDlg m_progressDlg = new ProgressDlg(m_mainFrame);
    private MyFileChooser m_modelFileChooser;
    private final MyFileChooser m_openFileChooser;
    private final MyFileChooser m_saveFileChooser;

    public MainView(MainApp manager) {
        m_manager = manager;
        //进度条
        m_progressDlg.setVisible(false);
        //文件选择器
        String path = System.getProperty("user.dir");
        File curFile = new File(path);
        m_modelFileChooser = new MyFileChooser(curFile,".model","Model files(*.model)");
        m_openFileChooser = new MyFileChooser(curFile,".csv", "Csv files (*.csv)");
        m_saveFileChooser = new MyFileChooser(curFile,".csv", "Csv files (*.csv)");
    }

    public void showWindow() {
        m_mainFrame.setVisible(true);
    }

    public void addModelAction() {
        int retVal = m_modelFileChooser.showOpenDialog(m_mainFrame);
        if (retVal != MyFileChooser.APPROVE_OPTION) {
            return;
        }
        File file = m_modelFileChooser.getSelectedFile();
        if (!file.exists()) {
            return;
        }
        try {
            //ObjectInputStream input = new ObjectInputStream(new FileInputStream(file.getPath()));
            InputStream is = new FileInputStream(file);
            if (file.getName().endsWith(".gz")) {
                is = new GZIPInputStream(is);
            }
            ObjectInputStream input = new ObjectInputStream(is);
            Model model = (Model) input.readObject();
            input.close();
            boolean bEnabled = m_manager.checkModelCapabilities(model);
            m_mainFrame.addModel(model, m_modelFileChooser.getSelectedFile().getAbsolutePath(), bEnabled);

        } catch (Exception ex) {
            ex.printStackTrace();
            this.showErrorDlg(ex);
        }
    }

    public Boolean checkModelCapabilities(Model get) {
        return m_manager.checkModelCapabilities(get);
    }

    public void showErrorDlg(Exception ex) {
        JOptionPane.showMessageDialog(m_mainFrame,
                "Problem :" + ex.getMessage(),
                "CBIB Predictor",
                JOptionPane.ERROR_MESSAGE);
    }

    public void OpenNewDataFile() {
        m_manager.openNewDataFile();
    }

    public Instances getLoaderData() throws IOException {
        if (m_openFileChooser == null) {
            return null;
        }
        int retVal = m_openFileChooser.showOpenDialog(m_mainFrame);
        if (retVal != JFileChooser.APPROVE_OPTION) {
            return null;
        }
        CSVLoader loader = new CSVLoader();
        File currFile = loader.retrieveFile();
        String filename = m_openFileChooser.getSelectedFile().getAbsolutePath();
        if ((currFile == null) || (!currFile.getAbsolutePath().equals(filename))) {
            loader.setFile(new File(filename));
        }
        if (loader != null) {
            Instances data = loader.getDataSet();
            return data;
        }
        return null;
    }

    public void openNewInstances(Instances data) {
        m_mainFrame.setOpenData(data);
        m_mainFrame.setTitle("CBIB Predictor - "+getLoaderFileName());
        m_mainFrame.updateModelCapabilities();
        m_mainFrame.setSaveButtonEnabeled(false);
    }

    public void setResultData(Instances resultData) {
        if (resultData == null) {
            return;
        }
        m_mainFrame.setResultData(resultData);
        m_mainFrame.setSaveButtonEnabeled(true);
    }

    public void setErrorRow(int i,int j){
        m_mainFrame.setErrorRow(i,j);
    }

    public void setDataClassIndex(int i) {
        m_manager.setDataClassIndex(i);
    }

    public void startPredict(Model model) {
        m_manager.runDataPredictThread(model);
    }

    public void showProcessDlg(boolean b) {
        m_progressDlg.setProgress(0);
        m_progressDlg.setLocationRelativeTo(m_mainFrame);
        m_progressDlg.setVisible(b);
    }

    public boolean saveResultData() {
        return m_manager.saveResultData();
    }

    public String getSavePath() {
        return m_saveFileChooser.getSelectedFile().getAbsolutePath();
    }

    public boolean showSaveDlg() {
        int retVal = m_saveFileChooser.showSaveDialog(m_mainFrame);
        if (retVal != JFileChooser.APPROVE_OPTION) {
            return false;
        }
        return true;
    }

    public void setProgress(int i) {
        m_progressDlg.setProgress(i);
    }

    public void setStatusTxt(String string) {
        m_mainFrame.setStatusTxt(string);
    }

    public void setCorrectPercent(String string) {
        m_mainFrame.setCorrectPercent(string);
    }

    public String getLoaderFileName() {
        return m_openFileChooser.getSelectedFile().getAbsolutePath();
    }

    public boolean isModified() {
        return m_manager.isModified();
    }

    public void cancleWorker() {
        m_manager.cancleWorker();
    }
}
